//
//  HJTabbar.m
//  KaiChat
//
//  Created by lx on 2023/11/18.
//

#import "HJTabBarContentView.h"


@interface HJTabBarButton ()

@property (nonatomic, assign) NSInteger index;
@property (nonatomic, weak) UIImageView *imageView;
@property (nonatomic, weak) UILabel *titleLabel;
@property (nonatomic, strong) NSDictionary *dict;
@property (nonatomic, strong) UIImage *selectImage;
@property (nonatomic, strong) UIImage *image;
@property (nonatomic, strong) HJUnReadView *unReadView;

@end

@implementation HJTabBarButton

- (void)setHighlighted:(BOOL)highlighted {
    
}


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:CGRectZero]) {
        
        UIImageView *imageView = [UIImageView addToView:self];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.offset(27);
            make.centerX.offset(0);
            make.top.offset(6);
        }];
        
        UILabel *titleLabel = [UILabel lableWithSuperView:self font:UIFontMake(12) color:UIColor.tj_tabbarUnSelectColor title:nil textAlignment:NSTextAlignmentLeft];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.offset(0);
            make.top.equalTo(imageView.mas_bottom).offset(2);
        }];

        _imageView = imageView;
        _titleLabel = titleLabel;
    }
    return self;
}

- (HJUnReadView *)unReadView {
    if (!_unReadView) {
         _unReadView = [[HJUnReadView alloc] init];
        [self addSubview:_unReadView];
    }
    return _unReadView;
}
 
- (void)setBadgeValue:(NSInteger)badgeValue {
    _badgeValue = badgeValue;
    
    [self.unReadView setNum:badgeValue];
    CGSize unReadViewSize = _unReadView.tj_size;
    [self.unReadView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.imageView).offset(18);
        make.top.equalTo(self.imageView);
        make.width.offset(unReadViewSize.width);
        make.height.offset(unReadViewSize.height);
    }];
}


- (void)setDict:(NSDictionary *)dict {
    _dict = dict;
    
    _image = [UIImage imageNamed:dict[@"iconName"]];
    _selectImage = [UIImage imageNamed:dict[@"selectIconName"]];
    
    _imageView.image = _image;
    _titleLabel.text = dict[@"title"];
    _titleLabel.textColor = UIColor.tj_tabbarUnSelectColor;
}
 

- (void)setSelected:(BOOL)selected {
    [super setSelected:selected];
    
    if (selected) {
        _imageView.image = _selectImage;
        _titleLabel.textColor = UIColor.tj_tabbarSelectColor;
    } else {
        _imageView.image = _image;
        _titleLabel.textColor = UIColor.tj_tabbarUnSelectColor;
    }
}


@end

 

@interface HJTabBarContentView()

@property (nonatomic, weak) UIView *contentView;
@property (nonatomic, weak) HJTabBarButton *lastClickTabBarButton;
@property (nonatomic, strong) NSMutableArray *tabBarButtons;

@end


@implementation HJTabBarContentView


- (HJTabBarContentView *)initWithDatas:(NSArray *)datas {
    if (self = [super initWithFrame:CGRectZero]) {
        
        _contentView = [UIView addToView:self];
        _tabBarButtons = [NSMutableArray array];
        
        [datas enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            HJTabBarButton *tabBarButton = [[HJTabBarButton alloc] init];
            tabBarButton.dict = obj;
            tabBarButton.index = idx;
            [tabBarButton addTarget:self action:@selector(didClickTabBarButton:) forControlEvents:UIControlEventTouchUpInside];
            [self.contentView addSubview:tabBarButton];
            [self.tabBarButtons addObject:tabBarButton];
        }];
        
        _lastClickTabBarButton = _tabBarButtons.firstObject;
        _lastClickTabBarButton.selected = YES;
        
        UIView *lineView = [UIView addToView:self];
        lineView.backgroundColor = UIColor.tj_separatorColor;
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.offset(0);
            make.height.offset(0.7);
        }];
        
    }
    return self;
}

- (HJTabBarButton *)buttonWithIndex:(NSInteger)index {
    if (index >= _tabBarButtons.count) return nil;
    return _tabBarButtons[index];
}

- (void)didClickTabBarButton:(HJTabBarButton *)button {
    
    if (_lastClickTabBarButton == button) return;
    
    _lastClickTabBarButton.selected = NO;
    button.selected = YES;
    _lastClickTabBarButton = button;
    
    if ([self.delegate respondsToSelector:@selector(tabBar:didSelectButton:index:)]) {
        [self.delegate tabBar:self didSelectButton:button index:button.index];
    }
}

- (void)selectIndex:(NSInteger)index {
    if (index >= _tabBarButtons.count) return;
    [self didClickTabBarButton:_tabBarButtons[index]];
}
 
- (void)layoutSubviews {
    [super layoutSubviews];
    
    _contentView.frame = CGRectMake(0, 0, self.frame.size.width, 49);
    CGFloat leftMargin = 0;
    CGFloat itemW = (_contentView.frame.size.width - leftMargin * 2) / _tabBarButtons.count;
    [_tabBarButtons enumerateObjectsUsingBlock:^(HJTabBarButton *button, NSUInteger idx, BOOL * _Nonnull stop) {
        button.frame = CGRectMake(leftMargin + itemW * idx, 0, itemW, _contentView.frame.size.height);
    }];
}

 
@end
